﻿package com.kasa.gps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.kasa.gps.db.table.LocationInfoTableAdapter;
import com.kasa.gps.db.table.VehiclesInfoTableAdapter;
import com.kasa.gps.service.TrackService;
import com.kasa.gps.tool.Tool;

public class OrderGPSActivity extends Activity{

	TableLayout mainLayout = null;
	
	public static final byte ITEMID_NEW_SETTING = 0;
	public static final byte ITEMID_UPDATE_SETTING = 1;
	private static final byte ITEMID_HISTORY_TRACK = 2;
	private static final byte ITEMID_HELPS = 3;
	private static final byte ITEMID_ABOUT = 4;
	private static final byte ITEMID_EXIT = 5;
	
	private boolean canUpdate = false;
	
	private Spinner licensePlates_text = null;
	private Spinner transportPlans_text = null;
	private TextView expectedTime_text = null;
	private static TextView longitude_text = null;
	private static TextView latitude_text = null;
	private static TextView area_text  = null;
	private static TextView stree_text = null;
	private static TextView time_text  = null;
	private static TextView speed_text = null;
	private static TextView remarks_text = null;
	
	private VehiclesInfoTableAdapter vita = null;
	private static Button uploadByHand = null;
	private Button trackService = null;
	
	private Map<String,List<String>> LicensePlatesAndTransportPlans = new HashMap<String, List<String>>();
	private Map<String,String> expectedTimeMap = new HashMap<String, String>();
	private Map<String,Integer> vehiclesInfoIdMap = new HashMap<String, Integer>();
	
	private ArrayAdapter<Object> licensePlates_adapter = null;
	private ArrayAdapter<String> transportPlans_adapter = null;
	
	private int vehiclesInfoId = 0;
	
	private boolean isServiceRunning = false;
	private static Context applicationContext = null;
	
	//进度条
	private static ProgressDialog loadingMap = null;
	private static boolean canreInitialized = false;
	private SharedPreferences sharedPreferences = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("OrderGPSActivity", "onCreate..."+savedInstanceState);
		
		ScrollView verticalScrollView = new ScrollView(this);
		HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
		verticalScrollView.addView(horizontalScrollView);
		
		mainLayout = new TableLayout(this);
		mainLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		horizontalScrollView.addView(mainLayout);
		
		vita = new VehiclesInfoTableAdapter(this);
		isServiceRunning = Tool.isServiceRunning(this, TrackService.class.getName());
		
		setMainLayout();
		setContentView(verticalScrollView);
		setListener();
		
		applicationContext = getApplicationContext();
		
		sharedPreferences = getSharedPreferences("ordergpsPreferences", Context.MODE_PRIVATE);
		reInitialized(sharedPreferences);
	}
	
	
	private void reInitialized(SharedPreferences sharedPreferences){
		if(canreInitialized && sharedPreferences!=null && sharedPreferences.contains(VehiclesInfoTableAdapter.LICENSE_PLATES)){
			licensePlates_text.setSelection(licensePlates_adapter.getPosition(sharedPreferences.getString(VehiclesInfoTableAdapter.LICENSE_PLATES,"")));
			if(null != transportPlans_adapter)
			{
				transportPlans_text.setSelection(transportPlans_adapter.getPosition(sharedPreferences.getString(VehiclesInfoTableAdapter.TRANSPORT_PLANS, "")));
			}
			
			longitude_text.setText(sharedPreferences.getString(LocationInfoTableAdapter.LONGITUDE,""));
			latitude_text.setText(sharedPreferences.getString(LocationInfoTableAdapter.LATITUDE,""));
			area_text.setText(sharedPreferences.getString(LocationInfoTableAdapter.AREA,""));
			stree_text.setText(sharedPreferences.getString(LocationInfoTableAdapter.STREE,""));
			time_text.setText(sharedPreferences.getString(LocationInfoTableAdapter.UPDATETIME,""));
			canreInitialized = false;
		}
		
	}
		
	@Override
	protected void onPause() {
		super.onPause();
		if(licensePlates_text.getCount()<=0){
			return ;
		}
		
		Editor editor = sharedPreferences.edit();
		editor.putString(VehiclesInfoTableAdapter.LICENSE_PLATES, licensePlates_text.getSelectedItem().toString());
		if(null != transportPlans_text.getSelectedItem())
		{
			editor.putString(VehiclesInfoTableAdapter.TRANSPORT_PLANS, transportPlans_text.getSelectedItem().toString());
		}
		
		editor.putString(LocationInfoTableAdapter.LONGITUDE, longitude_text.getText().toString())
									.putString(LocationInfoTableAdapter.LATITUDE, latitude_text.getText().toString())
									.putString(LocationInfoTableAdapter.STREE, stree_text.getText().toString())
									.putString(LocationInfoTableAdapter.UPDATETIME, time_text.getText().toString()).commit();
		
		canreInitialized = true;
		Log.i("OrderGPSActivity", "onPause...");
	}
			
	private void setMainLayout(){
		
		//手工上传
		TableRow op_row = new TableRow(this);
		op_row.setGravity(Gravity.CENTER);
		mainLayout.addView(op_row);
		//
		uploadByHand = new Button(this);
		uploadByHand.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		uploadByHand.setText(R.string.upload_by_hand_title);
		uploadByHand.setEnabled(false);
		op_row.addView(uploadByHand);
		//开启跟踪服务
		trackService = new Button(this);
		trackService.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		trackService.setEnabled(false);
		op_row.addView(trackService);
		
		//车牌
		TableRow licensePlates_row = new TableRow(this);
		licensePlates_row.setGravity(Gravity.CENTER);
		mainLayout.addView(licensePlates_row);
		//
		TextView licensePlates_title = new TextView(this);
		licensePlates_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		licensePlates_title.setText(R.string.licensePlates_title);
		licensePlates_row.addView(licensePlates_title);
		//
		licensePlates_text = new Spinner(this);
		licensePlates_row.addView(licensePlates_text);
		
		//运输计划
		TableRow transportPlans_row = new TableRow(this);
		transportPlans_row.setGravity(Gravity.CENTER);
		mainLayout.addView(transportPlans_row);
		//
		TextView transportPlans_title = new TextView(this);
		transportPlans_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		transportPlans_title.setText(R.string.transportplans_title);
		transportPlans_row.addView(transportPlans_title);
		//
		transportPlans_text = new Spinner(this);
		transportPlans_row.addView(transportPlans_text);
		
		//计划完成时间
		TableRow expectedTime_row = new TableRow(this);
		expectedTime_row.setGravity(Gravity.CENTER);
		mainLayout.addView(expectedTime_row);
		//
		TextView expectedTime_title = new TextView(this);
		expectedTime_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		expectedTime_title.setText(R.string.expectedtime_title);
		expectedTime_row.addView(expectedTime_title);
		//
		expectedTime_text = new Button(this);
		expectedTime_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		expectedTime_row.addView(expectedTime_text);
		
		//位置更新
		//经度
		TableRow longitude_row = new TableRow(this);
		longitude_row.setGravity(Gravity.CENTER);
		mainLayout.addView(longitude_row);
		//
		TextView longitude_title = new TextView(this);
		longitude_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		longitude_title.setText(R.string.longitude_title);
		longitude_row.addView(longitude_title);
		//
		longitude_text = new Button(this);
		longitude_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		//longitude_text.setText("123.456789");
		longitude_text.setEllipsize(TruncateAt.MARQUEE);//跑马灯
		longitude_text.setMarqueeRepeatLimit(1000);//跑马灯重复次数
		longitude_row.addView(longitude_text);
		
		//纬度
		TableRow latitude_row = new TableRow(this);
		latitude_row.setGravity(Gravity.CENTER);
		mainLayout.addView(latitude_row);
		//
		TextView latitude_title = new TextView(this);
		latitude_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		latitude_title.setText(R.string.latitude_title);
		latitude_row.addView(latitude_title);
		//
		latitude_text = new Button(this);
		latitude_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		//latitude_text.setText("23.145678");
		latitude_text.setEllipsize(TruncateAt.MARQUEE);//跑马灯
		latitude_text.setMarqueeRepeatLimit(1000);//跑马灯重复次数
		latitude_row.addView(latitude_text);
		
		//area
		TableRow area_row = new TableRow(this);
		area_row.setGravity(Gravity.CENTER);
		mainLayout.addView(area_row);
		//
		TextView area_title = new TextView(this);
		area_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		area_title.setText(R.string.area_title);
		area_row.addView(area_title);
		//
		area_text = new Button(this);
		area_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		//area_text.setText("中国广东省深圳市南山区高新园");
		//area_text.setWidth(150);//设定宽度
		area_text.setEllipsize(TruncateAt.MARQUEE);//跑马灯
		area_text.setMarqueeRepeatLimit(1000);//跑马灯重复次数
		area_text.setSingleLine();//单行
		area_row.addView(area_text);
		
		//stree
		TableRow stree_row = new TableRow(this);
		stree_row.setGravity(Gravity.CENTER);
		mainLayout.addView(stree_row);
		//
		TextView stree_title = new TextView(this);
		stree_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		stree_title.setText(R.string.stree_title);
		stree_row.addView(stree_title);
		//
		stree_text = new Button(this);
		stree_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		//stree_text.setText("科苑大道一路");
		stree_row.addView(stree_text);
		
		//time
		TableRow time_row = new TableRow(this);
		time_row.setGravity(Gravity.CENTER);
		mainLayout.addView(time_row);
		//
		TextView time_title = new TextView(this);
		time_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		time_title.setText(R.string.time_title);
		time_row.addView(time_title);
		//
		time_text = new Button(this);
		time_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		//time_text.setText("2011-05-16 14:16:09");
		time_row.addView(time_text);
		
		//speed
		TableRow speed_row = new TableRow(this);
		speed_row.setGravity(Gravity.CENTER);
		mainLayout.addView(speed_row);
		//
		TextView speed_title = new TextView(this);
		speed_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		speed_title.setText(R.string.speed_title);
		speed_row.addView(speed_title);
		//
		speed_text = new Button(this);
		speed_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		speed_row.addView(speed_text);
		
		//备注
		TableRow remarks_row = new TableRow(this);
		remarks_row.setGravity(Gravity.CENTER);
		mainLayout.addView(remarks_row);
		//
		TextView remarks_title = new TextView(this);
		remarks_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		remarks_title.setText(R.string.remarks_title);
		remarks_row.addView(remarks_title);
		//
		remarks_text = new EditText(this);
		remarks_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		remarks_row.addView(remarks_text);
		
		
		if(isServiceRunning){
			trackService.setText(R.string.trackservice_close_title);
			licensePlates_text.setEnabled(false);
			transportPlans_text.setEnabled(false);
		}else{
			trackService.setText(R.string.trackservice_open_title);
			licensePlates_text.setEnabled(true);
			transportPlans_text.setEnabled(true);
			
		}
		
		Cursor cursor = vita.getAllLicensePlatesAndTransportPlans();
		int rowNum = cursor.getCount();
		if(rowNum > 0){
			canUpdate = true;
			cursor.moveToFirst();
			int vehiclesInfoId_col_index = cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.ID);
			int LICENSE_PLATES_col_index = cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.LICENSE_PLATES);
			int TRANSPORT_PLANS_col_index = cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.TRANSPORT_PLANS);
			int EXPECTED_TIME_col_index = cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.EXPECTED_TIME); 
			do{
				String license_plates = cursor.getString(LICENSE_PLATES_col_index);
				if(!LicensePlatesAndTransportPlans.containsKey(license_plates)){
					LicensePlatesAndTransportPlans.put(license_plates, new ArrayList<String>());
				}
				String transport_plans = cursor.getString(TRANSPORT_PLANS_col_index);
				LicensePlatesAndTransportPlans.get(license_plates).add(transport_plans);
				vehiclesInfoIdMap.put(license_plates+transport_plans, cursor.getInt(vehiclesInfoId_col_index));
				expectedTimeMap.put(license_plates+transport_plans, cursor.getString(EXPECTED_TIME_col_index));
			}while(cursor.moveToNext());
			
			licensePlates_adapter = new ArrayAdapter<Object>(OrderGPSActivity.this, android.R.layout.simple_spinner_item,LicensePlatesAndTransportPlans.keySet().toArray());
			licensePlates_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			licensePlates_text.setAdapter(licensePlates_adapter);
//			if(getIntentFromService!=null){
//				Log.i("getIntentFromService", "getIntentFromService...licensePlates_text");
//				licensePlates_text.setSelection(licensePlates_adapter.getPosition(getIntentFromService.getStringExtra(VehiclesInfoTableAdapter.LICENSE_PLATES))) ;
//				licensePlates_text.setEnabled(false);
//			}
			
			
		}else{
			licensePlates_text.setEnabled(false);
			transportPlans_text.setEnabled(false);
		}
		cursor.close();
		vita.close();
		
	}

	private void setListener(){
		licensePlates_text.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

				transportPlans_adapter = new ArrayAdapter<String>(OrderGPSActivity.this, android.R.layout.simple_spinner_item,LicensePlatesAndTransportPlans.get(licensePlates_text.getSelectedItem().toString()));
				transportPlans_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				transportPlans_text.setAdapter(transportPlans_adapter);
				transportPlans_text.setSelection(0);
				//transportPlans_text.setEnabled(true);
							
//				if(getIntentFromService!=null){
//					Log.i("getIntentFromService", "getIntentFromService...transportPlans_text");
//					transportPlans_text.setSelection(transportPlans_adapter.getPosition(getIntentFromService.getStringExtra(VehiclesInfoTableAdapter.TRANSPORT_PLANS)));
//					transportPlans_text.setEnabled(false);
//				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		transportPlans_text.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				String key = licensePlates_text.getSelectedItem().toString()+transportPlans_text.getSelectedItem().toString();
				expectedTime_text.setText(expectedTimeMap.get(key));
				vehiclesInfoId = vehiclesInfoIdMap.get(key);
				trackService.setEnabled(true);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
		uploadByHand.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//向后台服务发送数据上传的消息
				//new TrackService.UploadDataHandler().sendEmptyMessage(0); 
				Bundle data = new Bundle();
				data.putBoolean(Tool.CLOSE_SERVICE_KEY, false);
				data.putString(Tool.REMARKS_TEXT_KEY, remarks_text.getText().toString());
				TrackService.TrackServiceHandler handler = new TrackService.TrackServiceHandler();
				Message msg = handler.obtainMessage(handler.WHAT_UPLOAD_0);
				msg.setData(data);
				handler.sendMessage(msg);
			}
		});
		
		trackService.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent service = new Intent(OrderGPSActivity.this,TrackService.class);
				
				if(Tool.isServiceRunning(OrderGPSActivity.this, TrackService.class.getName())){
					if(stopService(service)){
						trackService.setText(R.string.trackservice_open_title);
						licensePlates_text.setEnabled(true);
						transportPlans_text.setEnabled(true);
						Tool.showMessage(OrderGPSActivity.this, getString(R.string.close_service_successful));
					}else{
						Tool.showMessage(OrderGPSActivity.this, getString(R.string.close_service_unsuccessful));
					}
					
				}else{
					//如果要开启服务，先检查网络是否可用
					//if(Tool.isNetworkOK(OrderGPSActivity.this)){
						service.putExtra(VehiclesInfoTableAdapter.ID, vehiclesInfoId);
						service.putExtra(VehiclesInfoTableAdapter.LICENSE_PLATES, licensePlates_text.getSelectedItem().toString());
						service.putExtra(VehiclesInfoTableAdapter.TRANSPORT_PLANS, transportPlans_text.getSelectedItem().toString());
						service.putExtra(VehiclesInfoTableAdapter.EXPECTED_TIME, expectedTime_text.getText());
						
						if(startService(service)!=null){
							trackService.setText(R.string.trackservice_close_title);
							licensePlates_text.setEnabled(false);
							transportPlans_text.setEnabled(false);
							Tool.showMessage(OrderGPSActivity.this, getString(R.string.open_service_successful));
						}else{
							Tool.showMessage(OrderGPSActivity.this, getString(R.string.open_service_unsuccessful));
						}
					//}else{
						//提示打开网络
//						AlertDialog.Builder networkDialog = new AlertDialog.Builder(OrderGPSActivity.this);
//						networkDialog
//										.setTitle(R.string.checked_network_disabled_title)
//										.setMessage(R.string.checked_network_disabled_msg)
//										.setPositiveButton(R.string.dialog_positivebutton_text, null)
//										.show();
										
					//}
				}
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu.add(0, ITEMID_NEW_SETTING, 0, R.string.menu_new_setting_title).setIcon(R.drawable.ic_menu_add);
		if(canUpdate){
			menu.add(0, ITEMID_UPDATE_SETTING, 0, R.string.menu_update_setting_title).setIcon(R.drawable.ic_menu_update).setEnabled(true);
		}else{
			menu.add(0, ITEMID_UPDATE_SETTING, 0, R.string.menu_update_setting_title).setIcon(R.drawable.ic_menu_update).setEnabled(false);
		}
		menu.add(0, ITEMID_HISTORY_TRACK, 0, R.string.menu_history_track_title).setIcon(R.drawable.ic_menu_history_track);
		menu.add(0, ITEMID_HELPS, 0, R.string.menu_helps_title).setIcon(R.drawable.ic_menu_help);
		menu.add(0, ITEMID_ABOUT, 0, R.string.menu_about_title).setIcon(R.drawable.ic_menu_about);
		menu.add(0, ITEMID_EXIT, 0, R.string.menu_exit_title).setIcon(R.drawable.ic_menu_exit);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch(item.getItemId()){
		
		//新建设置
		case ITEMID_NEW_SETTING:
			Intent new_setting = new Intent(OrderGPSActivity.this,SettingActivity.class);
			//new_setting.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			new_setting.putExtra(SettingActivity.SETTING_OP_KEY ,ITEMID_NEW_SETTING);
			new_setting.putExtra(VehiclesInfoTableAdapter.LICENSE_PLATES, LicensePlatesAndTransportPlans.keySet().toArray());
			startActivity(new_setting);
			return true;
		
		//更新设置
		case ITEMID_UPDATE_SETTING:
			Intent update_setting = new Intent(OrderGPSActivity.this,SettingActivity.class);
			//update_setting.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			//操作类型
			update_setting.putExtra(SettingActivity.SETTING_OP_KEY ,ITEMID_UPDATE_SETTING);
			//车牌号码
			update_setting.putExtra(VehiclesInfoTableAdapter.LICENSE_PLATES ,licensePlates_text.getSelectedItem().toString());
			//运输计划
			update_setting.putExtra(VehiclesInfoTableAdapter.TRANSPORT_PLANS ,transportPlans_text.getSelectedItem().toString());
			startActivity(update_setting);
			return true;
		//地图
		case ITEMID_HISTORY_TRACK:
			Intent mapIntent = new Intent(OrderGPSActivity.this,OrderGPSMapActivity.class);
			mapIntent.putExtra(LocationInfoTableAdapter.LONGITUDE, longitude_text.getText());
			mapIntent.putExtra(LocationInfoTableAdapter.LATITUDE, latitude_text.getText());
			//mapIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(mapIntent);
			//进度条
			loadingMap = ProgressDialog.show(OrderGPSActivity.this, getString(R.string.loadingmap_dialog_title), getString(R.string.loadingmap_dialog_msg));
			
		//帮助
		case ITEMID_HELPS:
			//
			return true;
		//关于
		case ITEMID_ABOUT:
			//
			return true;
		//退出
		case ITEMID_EXIT:
			//
			//Tool.exitApp(OrderGPSActivity.this);
			finish();
			return true;
			
		}
		return false;
	}
	public static class LocationHandler extends Handler {
		
		public final byte WHAT_UPDATE_MAINUI_0 = 0;
		public final byte WHAT_UPLOAD_SUCCESS_1 = 1;
		public final byte WHAT_TO_MAINUI_2 = 2;
		public final byte WHAT_PROVIDER_DISABLED_3 = 3;
		public final byte WHAT_PROVIDER_ENABLED_4 = 4;
		public final byte WHAT_PROVIDER_STATUS_CHANGED_5 = 5;
		
		public final byte WHAT_LOADINGMAP_6 = 6;
		
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == WHAT_UPDATE_MAINUI_0){
				Bundle data= msg.getData();
				longitude_text.setText(data.getString(LocationInfoTableAdapter.LONGITUDE));
				latitude_text.setText(data.getString(LocationInfoTableAdapter.LATITUDE));
				area_text.setText(data.getString(LocationInfoTableAdapter.AREA));
				stree_text.setText(data.getString(LocationInfoTableAdapter.STREE));
				time_text.setText(data.getString(LocationInfoTableAdapter.UPDATETIME));
				speed_text.setText(data.getString(LocationInfoTableAdapter.SPEED));
				uploadByHand.setEnabled(true);
			}else if(msg.what == WHAT_UPLOAD_SUCCESS_1){
				uploadByHand.setEnabled(false);
				//如果上传命令是由时间到而强制进行的，则关闭后台服务
				Bundle data = msg.getData();
				if(data.getBoolean(Tool.CLOSE_SERVICE_KEY)){
					Intent service = new Intent(applicationContext, TrackService.class);
					if(applicationContext.stopService(service)){
						Tool.showMessage(applicationContext, applicationContext.getString(R.string.close_service_successful));
						Intent mainUI = new Intent(applicationContext, OrderGPSActivity.class);
						mainUI.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						applicationContext.startActivity(mainUI);
					}
				}			
			} else if(msg.what == WHAT_TO_MAINUI_2){
				Intent mainUI = new Intent(applicationContext, OrderGPSActivity.class);
				mainUI.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				applicationContext.startActivity(mainUI);
				
				
			}else if(msg.what == WHAT_PROVIDER_DISABLED_3){
				Tool.showMessage(applicationContext, applicationContext.getString(R.string.provider_disabled_msg));
			}else if(msg.what == WHAT_PROVIDER_ENABLED_4){
				Tool.showMessage(applicationContext, applicationContext.getString(R.string.provider_enabled_msg));
			}else if(msg.what == WHAT_PROVIDER_STATUS_CHANGED_5){
				int status = msg.getData().getInt("StatusChanged");
				String msgstr = "";
				if(status == LocationProvider.OUT_OF_SERVICE){
					msgstr = applicationContext.getString(R.string.provider_status_out_of_service_msg);
				}else if(status == LocationProvider.AVAILABLE){
					msgstr = applicationContext.getString(R.string.provider_status_available_msg);
				}else if(status == LocationProvider.TEMPORARILY_UNAVAILABLE){
					msgstr = applicationContext.getString(R.string.provider_status_temporarily_unavailable_msg);
				}
				Tool.showMessage(applicationContext, msgstr);
			}else if(msg.what == WHAT_LOADINGMAP_6){
				//销毁进度对话框
				loadingMap.dismiss();
				
			}
			
		}
		
	}
	
	
	private byte KEYCODE_BACK_NUM = 0;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.i("KEYCODE_BACK_NUM", "KEYCODE_BACK_NUM="+KEYCODE_BACK_NUM);
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(KEYCODE_BACK_NUM > 0){
				
				//Tool.exitApp(OrderGPSActivity.this);
				//ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
				//activityManager.killBackgroundProcesses(getPackageName());//需要配置权限android.permission.KILL_BACKGROUND_PROCESSES
				finish();
				return true;
			}
			
			KEYCODE_BACK_NUM ++ ;
			Tool.showMessage(this, getString(R.string.exit_app_msg));
		}else{
			KEYCODE_BACK_NUM = 0;
		}
		
		return false;
	}
}
